package swingxrcp.framework.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * 分页信息
 *
 * @author liangfei
 *
 */
public class Page<T> implements Collection<T> {

	private final Collection<T> collection;

	// 当前页数
	private final int currentPage; // 从0开始

	// 每页大小
	private final int pageSize;

	// 总大小
	private final int totalSize;

	// 总页数
	private final int totalPage;

	public Page(Collection<T> collection, int currentPage, int pageSize, int totalSize) {
		if (collection == null)
			collection = new ArrayList<T>(0);
		if (currentPage < 0)
			throw new IllegalArgumentException("currentPage < 0");
		if (pageSize <= 0)
			throw new IllegalArgumentException("pageSize <= 0");
		if (totalSize < 0)
			throw new IllegalArgumentException("totalSize < 0");
		int totalPage = totalSize / pageSize;
		if (totalSize % pageSize != 0)
			totalPage ++;
		if ((totalPage == 0 && currentPage != 0)
				|| (totalPage != 0 && currentPage >= totalPage))
			throw new IllegalArgumentException("currentPage >= totalPage");
		this.collection = collection;
		this.currentPage = currentPage;
		this.pageSize = pageSize;
		this.totalSize = totalSize;
		this.totalPage = totalPage;
	}

	/**
	 * 获取当前页号
	 *
	 * @return 当前页号
	 */
	public int getCurrentPage() {
		return currentPage;
	}

	/**
	 * 获取每页数据大小
	 *
	 * @return 每页数据大小
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * 获取总数据大小
	 *
	 * @return 总数据大小
	 */
	public int getTotalSize() {
		return totalSize;
	}

	/**
	 * 获取总页数
	 *
	 * @return 总页数
	 */
	public int getTotalPage() {
		return totalPage;
	}

	/**
	 * 获取最大页数
	 *
	 * @return 最大页数
	 */
	public int getMaxPage() {
		return (totalPage == 0 ? 0 : totalPage - 1);
	}

	/**
	 * 判断当前页是否为第一页
	 *
	 * @return 当前页是否为第一页
	 */
	public boolean isFirstPage() {
		return currentPage == 0;
	}

	/**
	 * 判断当前页是否为最后一页
	 *
	 * @return 当前页是否为最后一页
	 */
	public boolean isLastPage() {
		return currentPage == getMaxPage();
	}

	/**
	 * 获取页面数据
	 *
	 * @return 页面数据
	 */
	public Collection<T> getData() {
		return collection;
	}

	// ---- 代理数据集合类 ----

	public boolean add(T o) {
		return collection.add(o);
	}

	public boolean addAll(Collection<? extends T> c) {
		return collection.addAll(c);
	}

	public void clear() {
		collection.clear();
	}

	public boolean contains(Object o) {
		return collection.contains(o);
	}

	public boolean containsAll(Collection<?> c) {
		return collection.containsAll(c);
	}

	public boolean isEmpty() {
		return collection.isEmpty();
	}

	public Iterator<T> iterator() {
		return collection.iterator();
	}

	public boolean remove(Object o) {
		return collection.remove(o);
	}

	public boolean removeAll(Collection<?> c) {
		return collection.removeAll(c);
	}

	public boolean retainAll(Collection<?> c) {
		return collection.retainAll(c);
	}

	public int size() {
		return collection.size();
	}

	public Object[] toArray() {
		return collection.toArray();
	}

	public <A> A[] toArray(A[] a) {
		return collection.toArray(a);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((collection == null) ? 0 : collection.hashCode());
		result = prime * result + currentPage;
		result = prime * result + pageSize;
		result = prime * result + totalSize;
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final Page<T> other = (Page<T>) obj;
		if (collection == null) {
			if (other.collection != null)
				return false;
		} else if (!collection.equals(other.collection))
			return false;
		if (currentPage != other.currentPage)
			return false;
		if (pageSize != other.pageSize)
			return false;
		if (totalSize != other.totalSize)
			return false;
		return true;
	}

}
